package com.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.activityClient.util.PropertiesUtil;
import com.jftpc.FtpClient;

@SuppressWarnings("serial")
public class FileUploadServlet extends HttpServlet {

	private String uploadPath;
	private String uploadHostName;
	private Integer uploadPort;
	private String uploadUserName;
	private String uploadPassword;
	private String downloadPath;
	private String downloadHostName;
	private Integer downloadPort;
	private int maxPostSize = 1000 * 1024 * 1024;
	private final static String SPIT_STR = ";";

	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		uploadPath = PropertiesUtil.getString("uploadPath");
		uploadHostName = PropertiesUtil.getString("uploadHostName");
		uploadPort = Integer.parseInt(PropertiesUtil.getString("uploadPort"));
		uploadUserName = PropertiesUtil.getString("uploadUserName");
		uploadPassword = PropertiesUtil.getString("uploadPassword");
		
		downloadPath = PropertiesUtil.getString("downloadPath");
		downloadHostName = PropertiesUtil.getString("downloadHostName");
		downloadPort = Integer.parseInt(PropertiesUtil.getString("downloadPort"));
		String newName = "";
		res.setContentType("text/html;charset=UTF-8");
		req.setCharacterEncoding("UTF-8");

		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(1024 * 20);

		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setSizeMax(maxPostSize);
		String fileName = null;
		FtpClient fc = new FtpClient();
		try {
		boolean b = fc.connect(uploadHostName, uploadPort, uploadUserName,uploadPassword);
		fc.setWorkDirectory(uploadPath);
		
			List fileItems = upload.parseRequest(req);
			Iterator iter = fileItems.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				if (!item.isFormField()) {
					int index = item.getName().lastIndexOf(".");
					newName = UUID.randomUUID().toString().replace("-", "")+item.getName().substring(index);
						boolean isUpload = fc.upload(item.getInputStream(),newName);
						if(!isUpload){
							fileName = "上传失败，请检查";
							break;
						}else {
							fileName = "http://"+downloadHostName+":"+downloadPort+downloadPath + newName;
						}
						
					}
					
				}
			
		} catch (Exception e) {
			fileName = "连接失败，请检查";
			e.printStackTrace();
		}finally{
			fc.close();
			PrintWriter pw = res.getWriter();
			pw.write(fileName+SPIT_STR+newName);
			pw.close();
		}
		
	}

}
